package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.commons.exception.ProductException;
import com.woniuxy.commons.exception.code.ProductExceptionCode;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.controller.form.ProductForm;
import com.woniuxy.dao.model.ProductsInfo;
import com.woniuxy.dao.ProductsInfoDao;
import com.woniuxy.dao.model.StoreInfo;
import com.woniuxy.service.ProductsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.StoreInfoService;
import com.woniuxy.service.dto.ProductDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 餐品表 服务实现类
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@Service
public class ProductsInfoServiceImpl extends ServiceImpl<ProductsInfoDao, ProductsInfo> implements ProductsInfoService {
    @Autowired
    private StoreInfoService storeInfoService;

    /**
     * @param storeId 商家id
     * @return 餐品传输对象集合
     */
    @Override
    public List<ProductDto> getProductsByStoreId(Long storeId) {
        if (storeId != null) {
            if (storeInfoService.getById(storeId) == null) {
                throw new ProductException(ProductExceptionCode.PRODUCT_ERROR_STORE);
            }
        } else {
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_STORE);
        }
        QueryWrapper<ProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("storeId", storeId);
        return BeanCopyUtil.copyList(list(queryWrapper), ProductDto.class);
    }

    /**
     * @param productForm 获取餐品参数对象
     * @return boolean 餐品是否新增
     */
    @Override
    public boolean addProduct(ProductForm productForm) {
        if (productForm.getStoreId() == null)
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_STORE);
        if (storeInfoService.getById(productForm.getStoreId()) == null)
            throw new ProductException(ProductExceptionCode.PRODUCT_ERROR_STORE);
        if (productForm.getProductsName() == null ||
                productForm.getProductsName().equals(""))
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_NAME);
        ProductsInfo productsInfo = new ProductsInfo();
        productsInfo.setStoreId(productForm.getStoreId());
        productsInfo.setProductsName(productForm.getProductsName());
        productsInfo.setProductsIntr(productForm.getProductsIntr());
        return save(productsInfo);
    }

    /**
     * @param id 餐品id
     * @return boolean 餐品是否删除成功
     */
    @Override
    public boolean delProductById(Long id) {
        return removeById(id);
    }

    /**
     * @param productForm 接受餐品数据对象
     * @return 更新后的餐品传输对象
     */
    @Override
    public ProductDto updateProduct(ProductForm productForm) {
        UpdateWrapper<ProductsInfo> updateWrapper = null;
        if (productForm.getStoreId() == null)
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_STORE);
        if (storeInfoService.getById(productForm.getStoreId()) == null)
            throw new ProductException(ProductExceptionCode.PRODUCT_ERROR_STORE);
        if (productForm.getProductsName() == null ||
                productForm.getProductsName().equals(""))
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_NAME);
        if (productForm.getId() != null) {
            if (getById(productForm.getId()) == null)
                throw new ProductException(ProductExceptionCode.PRODUCT_NOT_ID);
            updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("storeId", productForm.getStoreId());
            updateWrapper.set("productsName", productForm.getProductsName());
            updateWrapper.set("productsIntr", productForm.getProductsIntr());
            updateWrapper.eq("id", productForm.getId());
        } else {
            throw new ProductException(ProductExceptionCode.PRODUCT_NOT_ID);
        }
        update(updateWrapper);
        return BeanCopyUtil.copyOne(getById(productForm.getId()), ProductDto.class);
    }
}
